﻿using System.Web;
using System.Web.Mvc;
using CourseSystem.Core.Enum;

namespace CourseSystem.Common.Helpers
{
    public class UserAuth : FilterAttribute, IAuthorizationFilter
    {
        public UserTypeEnum UserType = UserTypeEnum.Guess;
        public bool IsLimt = false;
        public bool IsNotExpert = false;
        public string Error = "";
        public void OnAuthorization(AuthorizationContext filterContext)
        {
            if (UserHelper.LoginUser != null)
            {
                if (IsLimt)
                {
                    if (UserHelper.LoginUser.UserType == UserType)
                    {
                        if (UserType == UserTypeEnum.Teacher && UserHelper.LoginUser.IsExpert)
                        {

                        }
                        else
                            if (IsNotExpert && UserHelper.LoginUser.IsExpert)
                            {

                            }
                            else
                                return;
                    }
                    goto End;
                }

                if (UserHelper.LoginUser.UserType >= UserType)
                {
                    if (UserType == UserTypeEnum.Teacher && UserHelper.LoginUser.IsExpert)
                    {

                    }
                    else
                        if (IsNotExpert && UserHelper.LoginUser.IsExpert)
                        {

                        }
                        else
                            return;
                }
            }
        //   filterContext.Controller.ViewData["errorMsg"] = "fsdfsdf";
        //   filterContext.Result = new HttpUnauthorizedResult();
        End:

            var re = new ViewResult { ViewName = "NotFound" };
            re.ViewData["errorMsg"] = "您没有浏览该模块的权限";
            HttpContext.Current.Session["errorMsg"] = "您没有浏览该模块的权限";
            filterContext.Result = new RedirectResult(UserHelper.NotAuthUrl());//re;

            //跳转到登录页面
            // filterContext.HttpContext.Response.Redirect("http://xxx.com/Account/Login");
        }
    }
}